package com.faner.fast.task;//package com.faner.fast.controller.task;
//
//import com.faner.fast.constant.QueueConstants;
//import com.faner.fast.exception.FailedException;
//import com.faner.fast.exception.HttpException;
//import com.faner.fast.upms.service.FileService;
//import com.faner.fast.util.RedisUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.redisson.Redisson;
//import org.redisson.api.RLock;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
///**
// * description: TaskController
// * date: 2020/12/4 11:58
// * author: faner
// */
//@Slf4j
//@Component
//@EnableScheduling
//public class TaskController {
//    @Autowired
//    RedisUtil redisUtil;
//    @Autowired
//    Redisson redisson;
//    @Autowired
//    private  FileService fileService;
//    /**默认是fixedDelay 上一次执行完毕时间后执行下一轮*/
////    @Scheduled(cron = "0/20 * * * * *")
//    public void run(){
//        if (redisUtil.lGetListSize(QueueConstants.PDF_QUEUE) > 0){
//            //加锁
//            RLock lock = redisson.getLock("task-"+QueueConstants.PDF_QUEUE);
//            int lockTimeLimit = 30;
//            try{
//                //等价于 lock.lock(), 默认为30s
//                lock.lock(lockTimeLimit, TimeUnit.SECONDS);
//                List<Object> objects = redisUtil.lGet(QueueConstants.PDF_QUEUE, 0, 1);
//
//                System.out.println(objects.get(0));
//                String reportId = "37ca8617a73945d7ae8ddad7eddd854e";
//                Integer uid = 204;
//                Boolean aBoolean = fileService.pdfCreate(reportId, uid);
//                if (!aBoolean){
//                    redisUtil.lSet(QueueConstants.PDF_QUEUE,"pdf|"+reportId+"|"+uid);
//                }
//                redisUtil.leftPop(QueueConstants.PDF_QUEUE);
//            } catch (Exception e) {
//                log.error("Error occurred, lock message:{}", e.getMessage());
//                if (e instanceof HttpException) {
//                    throw new FailedException(((HttpException) e).getCode());
//                }
//            } finally {
//                lock.unlock();
//            }
//        }
//    }
//}
